import { TOGGLE_IS_DONE, TOGGLE_TYPE } from '../action/actionTypes';
import { DEL_TODO } from '../action/actionTypes';

const initState = {
  list: [
    {
      id: 1,
      name: 'JS',
      isDone: false,
    },
    {
      id: 2,
      name: 'React',
      isDone: true,
    },
    {
      id: 3,
      name: 'Redux',
      isDone: false,
    },
  ],
  type: 'completed',
};
export default function todoReducer(state = initState, action) {
  const { type, payload } = action;
  switch (type) {
    case 'todo/add':
      return {
        ...state,
        list: [{ ...payload }, ...state.list],
      };
    case TOGGLE_IS_DONE:
      return {
        ...state,

        list: state.list.map((item) => ({
          ...item,

          isDone: item.isDone === payload ? !item.isDone : item.isDone,
        })),
      };

    case DEL_TODO:
      return {
        ...state,

        list: state.list.filter((item) => item.id !== payload),
      };

    case TOGGLE_TYPE:
      return {
        ...state,
        type: payload,
      };

    default:
      return state;
  }
}
